/*
1,获取用户信息
2，编辑用户信息
*/
$(function () {
    // console.log('有效');


    // 文件上传绑定change事件
    $('#exampleInputFile').change(function () {
        const file = this.files[0]
        $('.user_pic').prop("src", URL.createObjectURL(file))
    })


    //获取用户信息  get /admin/user/detail
    function getAdminUserDetail() {
        $.get('/admin/user/detail', function (res) {
            console.log(res);


            const { email, nickname, password, userPic, username, } = res.data
            $("[name='username']").val(username)
            $("[name='nickname']").val(nickname)
            $("[name='email']").val(email)
            $(".user_pic").prop('src', userPic)
            $("[name='password']").val(password)
        })
    }




    // getAdminUserDetail()
    function getEdit() {
        const fd = new FormData()
        // console.log(fd);


        const username = $("[name='username']").val()
        const nickname = $("[name='nickname']").val()
        const email = $("[name='email']").val()
        console.log(email);
        const userPic = $(".user_pic").val()
        const password = $("[name='password']").val()


        fd.append('username', username)
        fd.append('nickname', nickname)
        fd.append('email', email)
        fd.append('userPic', userPic)
        fd.append('password', password)


        $.ajax({
            url: '/admin/user/edit',
            type: 'post',
            data: fd,
            contentType: false,
            processData: false,
            success(res) {
                if (res.code === 200) {
                    getAdminUserDetail()
                }
            }
        })
    }
    // 给修改做点击事件
    $('.btn-edit').click(function () {
        getEdit()
    })
    getAdminUserDetail()
})